<template>
  <div>
    <transition-group
      enter-active-class="animate__animated animate__fadeIn"
      leave-active-class="animate__animated animate__fadeOut"
    >
      <div
        v-show="showAbs"
        class="header-abs"
        v-scroll="handleScroll"
        key="abs"
      >
        <span @click="backhome" class="iconfont header-abs-back">&#xe624;</span>
      </div>
      <div v-if="!showAbs" class="header-fixed" key="fixed">
        <span @click="backhome" class="iconfont header-fixed-back"
          >&#xe624;</span
        >
        {{sightName}}
      </div>
    </transition-group>
  </div>
</template>

<script>
export default {
  name: "Header",
  props:{
    sightName:{
      type:String
    }
  },
  data() {
    return {
      showAbs: true
    };
  },
  methods: {
    backhome() {
      this.$router.push({
        name: "Home"
      });
    },
    handleScroll() {
      // 获取滚动的距离
      const top = document.documentElement.scrollTop;
      //   显示、隐藏
      this.showAbs = top > 43 ? false : true;
    }
  },

  // 自定义指令
  directives: {
    scroll: {
      inserted(el, binding) {
        let f = function(evt) {
          // value，指令绑定的值
          if (binding.value(evt, el)) {
            window.removeEventListener("scroll", f);
          }
        };
        window.addEventListener("scroll", f);
      }
    }
  }
};
</script>

<style lang="less" scoped>
@import url("~@/assets/styles/varibles.less");
.header {
  &-abs {
    position: absolute;
    left: 0.2rem;
    top: 0.06rem;
    width: 0.8rem;
    height: 0.8rem;
    border-radius: 50%;
    background-color: rgba(0, 0, 0, 0.6);
    text-align: center;
    line-height: 0.8rem;
    z-index: 1;
    &-back {
      color: #fff;
      font-size: 0.4rem;
    }
  }
  &-fixed {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: @HeaderHight;
    font-size: 0.32rem;
    text-align: center;
    color: #fff;
    line-height: @HeaderHight;
    background-color: @BgColor;
    z-index: 9;
    &-back {
      position: absolute;
      left: 0.2rem;
    }
  }
}
</style>
